package cn.lsj.payment.controller;


import cn.lsj.common.Commons;
import cn.lsj.common.entity.Payment;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/payment" + Commons.SERVER_VERSION)
public class PaymentController {

    @Value("${server.port}")
    private String port;

    @GetMapping("/get/{id}")
    public ResponseEntity<Payment> payment(@PathVariable("id") Integer id) {
        Payment payment = new Payment(id, "支付成功");
        return ResponseEntity.ok(payment);
    }



    @GetMapping("/loadbalancer")
    public ResponseEntity<String> loadbalancer() {
        return ResponseEntity.ok(port);
    }

    @SneakyThrows
    @GetMapping("/timeout")
    public ResponseEntity<Payment> timeout() {
        TimeUnit.SECONDS.sleep(50);
        Payment payment = new Payment(111, "支付成功");
        return ResponseEntity.ok(payment);
    }

    @GetMapping("/gzip")
    public ResponseEntity<List<Payment>> gzip() {
        int len = 10000000;
        List<Payment> list = new ArrayList<>(len);
        for (int i = 0; i < len; i++) {
            list.add(Payment.builder().message("支付成功").id(11).build());
        }
        return ResponseEntity.ok(list);
    }
}
